<?php
class Default_Model_DbTable_Tour extends Zend_Db_Table_Abstract
{
	protected $_name="hae_tour";
	protected $_primary="id";
	protected $_sequence=true;
	public function gettour($page=1, $tourtypeId,$countryId, $regionId, $theme)
	{
	    //Locale
	    $locale = new HAE_Element_Locale();
	    $lang = $locale->getLocale();
	    
	    
		// genarate select //////
		$select = $this->select();
		$select->from($this->_name,array('id','name_'.$lang.' AS name','intro_'.$lang.' AS intro','desc_'.$lang.' AS des','image','length_day','length_night','alias'));
		if ($countryId!=0)
			$select->where('countryId = ?',$countryId);
		if ($tourtypeId!=0)
			$select->where('tourtypeId = ?',$tourtypeId);
		if ($regionId!=0)
			$select->where('regionId = ?',$regionId);
		if ($theme!=0)
		{
			$themes= explode ("_", $theme);
			$str="";
			for ($i=0;$i<count($themes)-1;$i++)
			{
				$str .= " theme LIKE '%". $themes[$i]."%'";
				if ($i <  (count($themes)-2))
					$str .=" OR ";
			}
			
			$select->where($str);
		}
		////  Paginator /////	
		$itemsPerPage = 4;
		$tour = new Zend_Paginator_Adapter_DbSelect($select);
		$tours = new Zend_Paginator($tour);
		$tours->setCurrentPageNumber($page);
		$tours->setItemCountPerPage($itemsPerPage);
		return $tours;
	}
	public function getTourId($id)
	{
		$id = (int)$id;
		$row = $this->fetchRow('id=' . $id);
		if(!$row)
		{
			throw  new Exception("Could not find row {$id}");
		}
		return $row->toArray();
	}
	 public function getsimilartour($cityId, $tourtypeId)
	 {
	     $select = $this->select();
	     if ($tourtypeId!=0)
	     	$select->where('tourtypeId = ?',$tourtypeId);
	     if ($cityId!=0)
	     	$select->where('cityId = ?',$cityId);
	     $select->limit(4);
	     return $this->fetchAll($select);
	 }
}
?>